// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import { globalType } from "../../../../script/util/globalType";
import { PanelInfo } from "../../../../script/util/panel/panel";
import { TagsCtrl } from "../../../../script/util/tags/tagsCtrl";
import { WinBase } from "../../../../script/wins/WinBase";
import card from "../../active/script/card";
import fun from "../../active/script/fun";


const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends WinBase {

    @property([PanelInfo])
    panelArray: PanelInfo[] = [];

    @property(cc.Sprite)
    panelBgSprite: cc.Sprite = null;

    @property(TagsCtrl)
    tagsCtrl:TagsCtrl = null;
    private _showPanels: globalType.KeyObject<PanelInfo> = {}; //存不同的面板
    private _selectTagKey:string = null; //当前选中的
    start () {
        //开始加载所有地图 读取配置表
        for(let i = 0, len = this.panelArray.length; i < len; i++) {
            let panel = this.panelArray[i];
            this._showPanels[panel.key] = panel;
        }
        this.onTagClick("fun");
    }
    onSelectKey(key:string) {
        this.tagsCtrl.selectKey(key);
    }
    canClick() {
        return true;
    }
    onTagClick(key:string) {
        console.log('onTagClick', key);
        if(this._selectTagKey) {
            this._showPanels[this._selectTagKey].node.active = false;
        }
        this._selectTagKey = key;
        let panelInfo = this._showPanels[this._selectTagKey];
        if(panelInfo) {
            if(!panelInfo.node) {
                panelInfo.node = cc.instantiate(panelInfo.pfb);
                this.node.addChild(panelInfo.node);
            }
            if(this.panelBgSprite && this.panelBgSprite.spriteFrame) {
                 this.panelBgSprite.spriteFrame = panelInfo.bgSpFrame;
            }
            panelInfo.node.active = true;
            this.updatePanel();
        }
    }

    updatePanel(effectType:number = 0) {
        let panelInfo = this._showPanels[this._selectTagKey];
        if(panelInfo && panelInfo.node) {
            switch(this._selectTagKey) {
                case 'fun':
                    panelInfo.node.getComponent(fun).updateItems(this._selectTagKey);
                    break;
                    case 'zhengjian':
                        // panelInfo.node.getComponent(card).updateItems(this._selectTagKey);
                        break;
                        default:
                            break;
                        // case 'swordtomb':
                        //     panelInfo.node.getComponent(SwordtombCtrl).updateInfo(true, effectType);
                        //     break;
                        //     case 'lifestyle':
                        //         panelInfo.node.getComponent(LifeStyleInfo).updateInfo();
                        //         break;
            }
        }
    }
}
